
*Let's start with loading the data and looking at some descriptive statistics.

clear all
use C:\Users\furdinez\Desktop\FRAN\USP\IRI\Met. Quantitativos II\Final Work/DB_1.dta


tabstat  ofcases, by( recognition) stats(mean sd n)
histogram  ofcases, discrete freq scheme(s1mono)

*For poisson and other models for count outcomes, prcounts extends the features of predict by//
*computing the predicted rate and predicted probabilities of each count from 0 to the specified maximum//
*for every observation.

prcounts psn, plot max(9)
label var psnobeq "Observed Proportion"
label var psnpreq "Poisson Prediction"
label var psnval "# of Articles"
list psnval psnobeq psnpreq in 1/10

*Tests for dispersion of count models: nbvargr and countfit
nbvargr ofcases, n(29)  
countfit ofcases recognition mchimworld tradeofgdp, prm nbreg

*This third test was not included in the paper

poisson  ofcases recognition, vce(robust)
estimates store RECOGNITION
poisson  ofcases  mchimworld, vce(robust)
estimates store SHARE
poisson  ofcases   tradeofgdp, vce(robust)
estimates store OPENNESS
estout RECOGNITION SHARE OPENNESS, cells(b(star fmt(2)) se(par fmt(2))) legend label varlabels (_cons Constant)


*Now we will proceed to comparar OLS with count models
regress  ofcases recognition mchimworld tradeofgdp, beta
*store the model
estimates store OLS


poisson ofcases recognition mchimworld tradeofgdp 
*store the model
estimates store Poisson

*we could ru poisson with vce(robust) to test a robust st error
poisson ofcases recognition mchimworld tradeofgdp, vce(robust) nolog

*Negative binomial regression 
nbreg ofcases recognition mchimworld tradeofgdp 
*store the model
estimates store NBRM

*Zero-Truncated Poisson Regression
ztp ofcases recognition mchimworld tradeofgdp
*store the model
estimates store ZTPR

*Finally Zero-truncated Negative Binomial Regression because we have overdispersion in addition to zero truncation.
ztnb ofcases recognition mchimworld tradeofgdp
*store the model
estimates store ZTNB

*to compare the models we run estout model_1 model_2 model_3 model_4
estout OLS Poisson NBRM ZTPR ZTNB

*we can add standard error, limit to two decimals the coefficents and add R2
estout OLS Poisson NBRM ZTPR ZTNB, cells(b(star fmt(2)) t(par fmt(2))) legend label varlabels (_cons Constant) stats(r2)

*we will run it withour R2
estout OLS Poisson NBRM ZTPR ZTNB, cells(b(star fmt(2)) t(par fmt(2))) legend label varlabels (_cons Constant)

*Another way of comparing the models is running esttab
esttab OLS Poisson NBRM ZTPR ZTNB, cells(b(star fmt(2)) t(par fmt(2))) legend label varlabels (_cons Constant) nonumbers mtitles("OLS" "PRM""NBRM""ZTPRM""ZTNBM")

*to read ZTNBM we can use estat ic 
estat ic 
*We run listcoef ti interpret the coefficients
listcoef recognition mchimworld tradeofgdp, percent help

*run a Poisson controlling for country, for Argentina y Brasil
sort country
by country: sum ofcases
by country: poisson ofcases recognition mchimworld tradeofgdp if countrycode==1
estimates store ARG
by country: poisson ofcases recognition mchimworld tradeofgdp if countrycode==3
estimates store BRA
esttab ARG BRA, cells(b(star fmt(2)) p(par fmt(2))) legend label varlabels (_cons Constant) nonumbers mtitles("Argentina" "Brazil")



*******************************
************GRAPHS*************
*******************************

*GRAPH 1

quietly regress ofcases mchimworld tradeofgdp recognition, beta 
predict hat
predict stf, stdf
predict res, r


twoway lfitci hat mchimworld if recognition==1 || lfitci hat mchimworld if recognition==0 || scatter  hat mchimworld if recognition==1 || scatter  hat mchimworld if recognition==0, name(a)
twoway lfitci hat tradeofgdp if recognition==1 || lfitci hat tradeofgdp if recognition==0 || scatter  hat tradeofgdp if recognition==1 || scatter  hat tradeofgdp if recognition==0, name(b)

graph combine a b


*GRAPH 2

scatter ofcases mchimworld if recognition==1, mlabel(country)
scatter ofcases tradeofgdp if recognition==1, mlabel(country)

*Using Margins for Predicted Probabilities

*GRAPH 1
poisson ofcases mchimworld tradeofgdp i.recognition
margins recognition, at(mchimworld=(0(0.05)0.25)) atmeans vsquish post
marginsplot, xdimension(at(mchimworld)) recast(line) recastci(rarea)

*GRAPH 2
poisson ofcases mchimworld tradeofgdp i.recognition
margins recognition, at(tradeofgdp=(0(20)150)) atmeans vsquish post
marginsplot, xdimension(at(tradeofgdp)) recast(line) recastci(rarea)
